(function(){

	var db = require("../db/index");
	var UserCon = require("./_con");
	var BusCon = require("../bus/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");

	var skill = {
		/*
		 *	购买商品：
		 *		u:下定单
		 *		b:生成订单
		 *		u:支付订单
		 *		b:处理订单（发货，修改价格，特殊服务）
		 *		u:评价商品 *
		 */
		//下订单
		//return ach
		makeAch:function (bus, goods_id){
			var user = this;
			console.log(user.info.name,"想要购买一个",goods_id,"”商品");
			try{
				var ach = bus.makeAchByGoodsId(user, goods_id);
				user.addAchs(ach._id);
				console.log(user.info.name,"下了一个“",goods_id,"”商品的订单");
			}catch(e){
				console.log(user.info.name,"下单失败！");
				throw e;
			}
			return ach;
		},
		//锁定订单，不再修改，解锁需要商家
		//锁定订单后通知商家收到订单
		lockAch:function(ach_id){
			var ach = AchCon.get(ach_id);
			ach.lock
		},
		//支付订单（第三方支付接口回掉后触发的函数）
		//并支持回掉到自己的线程上
		payAch:function(ach_id,callback){
			var payer = this;
			var ach = AchCon.get(ach_id);
			if (!ach) {
				console.log("订单编号“",ach_id,"”无效");
				throw "订单号无效";
			}else if(ach.info.state === "已付款"){
				throw "订单已支付"
			}
			var ach_user = UserCon.get(ach.info.user_id);
			var ach_bus = BusCon.get(ach.info.bus_id);
			var ach_goods = GoodsCon.get(ach.info.goods_id);
			ach.set_info({
				state:"已付款",
				payer_id:payer._id,
				pay_time:+new Date
			});
			db.update(config.dbname.ach,ach._id,ach);
			console.log(ach_user.info.name,"支付了订单号为：“",ach_id,"”的订单");

			callback&&callback.call(payer,ach_id);
			return ach;
		},
		//改变推荐积分
		_recInt:function(num){
			num = isNaN(num)?0:+num;
			return self.rec_int += num;
		},
		//改变购买积分
		_buyInt:function(num){
			num = isNaN(num)?0:+num;
			return self.buy_int += num;
		},
		//生成推荐链接
		buildRecommendGoodsURL:function (bus,goods_name) {
			var self = this;
			var url_hash = self._id.substr(2)+Math.random().toString(16).substr(2)+(+new Date).toString(16).substr(7);
			db.insert(config.dbname.RecommendGoodsURL,{
				_id:config.dbIdPrefix.RecommendGoodsURL+url_hash,
				user_id:self._id,
				bus_id:bus._id,
				goods_name:goods_name,
				url_hash:url_hash
			});
			console.log("用户",self.info.name,"推荐了商品“",goods_name,"”，推荐链接为：baseurl.com/"+url_hash);
			return url_hash;
		},
		//通过推荐链接中的推荐HASH来下订单
		makeAchWithRecommendURL:function(url_hash,goods){
			var self = this;
			var rgu_info = db.find_by_id(config.dbname.RecommendGoodsURL,config.dbIdPrefix.RecommendGoodsURL+url_hash);
			if (goods.info.name!==rgu_info.goods_name) {
				console.log("推荐的商品与购买的商品不匹配！推荐无效");
				return;
			}
			if (rgu_info) {
				var bus = BusCon.get(rgu_info.bus_id);
				//一般的下订单的方式
				var ach = self.makeAch(bus,goods._id);
				ach.info.url_hash = url_hash;
				ach.info.recInt = bus.getRecommendGoodsInt(rgu_info.goods_name);
				//添加回调
				//为推荐者添加积分
				ach.events.onPayed = require("../_event/BuyGoodsByRecommendURL_IncRecInt");
			}else{
				console.log("推荐标识符“",url_hash,"”无效！");
				console.log(db.find_all(config.dbname.RecommendGoodsURL,{}));
				db.insert(config.dbname.RecommendGoodsURL,{a:'xxx'});
				console.log(db.find_all(config.dbname.RecommendGoodsURL,{}));
			}
			return ach;
		},
		//获取收藏夹的数据
		getCollect:function(num,page){
			var result = (this.collect||[]).slice();
			data_cut.page(result,num,page);
			return result;
		},
		addGoodsToCollect:function(goods_id){
			var collect = (this.collect||(this.collect=[]));
			if (collect.every(function(other_goods_id){
				return other_goods_id!==goods_id
			})) {
				var result = collect.unshift(goods_id);
				this.collect = collect;
				db.update(config.dbname.user,this._id,this);
				return result;
			}else{
				return false;
			}
		},
		removeGoodsFromCollect:function (goods_id) {
			var collect = (this.collect||(this.collect=[]));
			var result = $Array.remove(collect,goods_id);
			if (result.length!==collect.length) {
				this.collect = result;
				db.update(config.dbname.user,this._id,this);
				return result;
			}
			return false;
		},
		getAchs:function(num,page){
			var result = (this.achs||(this.achs=[])).slice();
			data_cut.page(result,num,page);
			return result;
		},
		addAchs:function(ach_id){
			var achs = (this.achs||(this.achs=[])).slice();
			if (achs.every(function(other_ach_id){
				return other_ach_id!==ach_id
			})) {
				var result = achs.unshift(ach_id);
				this.achs = achs;
				db.update(config.dbname.user,this._id,this);
				return result;
			}else{
				return false;
			}
		},
		removeAch:function(ach_id){
			var achs = (this.achs||(this.achs=[]));
			var result = $Array.remove(achs,ach_id);
			if (result.length!==achs.length) {
				this.achs = result;
				db.update(config.dbname.user,this._id,this);
				return result;
			}
			return false;
		},
		getAddress:function(num,page){
			var result = (this.uas||(this.uas=[])).slice();
			data_cut.page(result,num,page);
			return result;
		},
		addAddress:function(ua_id){
			var uas = (this.uas||(this.uas=[])).slice();
			//地址的存储有上限限制
			if (uas.length>=config.userAddressNumber) {
				return false;
			}
			if (uas.every(function(other_ua_id){
				return other_ua_id!==ua_id
			})) {
				var result = uas.unshift(ua_id);
				this.uas = uas;
				db.update(config.dbname.user,this._id,this);
				return result;
			}else{
				return false;
			}
		},
		removeAddress:function(ua_id){
			var uas = (this.uas||(this.uas=[]));
			var result = $Array.remove(uas,ua_id);
			if (result.length!==uas.length) {
				if (this.default_ua_id===ua_id) {
					//默认收货地址被删除，则滞空
					this.default_ua_id = undefined;
				}
				this.uas = result;
				db.update(config.dbname.user,this._id,this);
				return result;
			}
			return false;
		},
		setDefaultAddress:function(address_id){
			if (this.uas&&this.uas.indexOf(address_id)!==-1) {
				this.default_ua_id = address_id;
				db.update(config.dbname.user,this._id,this);
			}else{
				throw "找不到指定的收获地址";
			}
		}
	}
	module.exports = skill;

}());
